﻿using System;
using System.Collections.Generic;
using System.Data;
using NetSurveillance.WebApp.DataServices.Models;
using ServiceStack.OrmLite;

namespace NetSurveillance.WebApp.DataServices.Implementations
{
    public class QQPasswordService : BaseService<QQPassword>
    {
        public QQPasswordService(string connStr, IOrmLiteDialectProvider provider = null)
            : base(connStr, provider)
        {
        }

        public override ServiceResult Save(QQPassword model)
        {
            using (var db = DbFactory.OpenDbConnection())
            {
                var modelInDb = db.SingleOrDefault<QQPassword>(" \"Account\" = {0}".Params(model.Account));
                if (modelInDb != null)
                {
                    modelInDb.StatusToken = 1;
                    modelInDb.LastModifiedTime = DateTime.Now;
                    modelInDb.Password = model.Password;
                    db.Save(modelInDb);
                    return ServiceResult.SuccessResult;
                }
                db.Save(model);
                return ServiceResult.SuccessResult;
            }
        }

        public override ServiceResult SaveAll(IEnumerable<QQPassword> models)
        {
            using (var db = DbFactory.OpenDbConnection())
            {
                using (var trans = db.OpenTransaction())
                {
                    try
                    {
                        foreach (var qqPass in models)
                        {
                            var modelInDb = db.SingleOrDefault<QQPassword>(" \"Account\" = {0}".Params(qqPass.Account));
                            if (modelInDb != null)
                            {
                                modelInDb.StatusToken = 1;
                                modelInDb.LastModifiedTime = DateTime.Now;
                                modelInDb.Password = qqPass.Password;
                                db.Update(modelInDb);
                            }
                            else
                            {
                                db.Insert(qqPass);
                            }
                        }
                        trans.Commit();
                    }
                    catch (Exception ex)
                    {
                        trans.Rollback();
                        return new ServiceResult(false, ex.Message);
                    }
                }
            }
            return ServiceResult.SuccessResult;
        }

        public ServiceResult Delete(long[] ids)
        {
            using (var db = DbFactory.OpenDbConnection())
            {
                using (var trans = db.OpenTransaction(IsolationLevel.ReadCommitted))
                {
                    try
                    {
                        foreach (long id in ids)
                        {
                            var password = db.Id<QQPassword>(id);
                            password.StatusToken = 0;
                            password.LastModifiedTime = DateTime.Now;
                            db.Update(password);
                        }
                    }
                    catch (Exception ex)
                    {
                        //todo:log ex
                        trans.Rollback();
                        return new ServiceResult(false, ex.Message);
                    }
                    trans.Commit();
                }
            }
            return ServiceResult.SuccessResult;
        }
    }
}
